#!/use/bin/python
# coding=utf-8
# 早间开机 数据分析-睡觉提醒-服务推送成功率
import redis
import datetime
import pymysql

from dbutils.pooled_db import PooledDB


# proactive_service_conf 数据源
def getConfConnection():
    # 开发环境
    # pool = PooledDB(pymysql, 1, host='172.20.135.96', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_conf',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.150.109', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                db='proactive_service_conf', port=3307)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur

# proactive_service_data 数据源
def getDataConnection():
    # 开发环境
    #pool = PooledDB(pymysql, 1, host='172.20.151.80', user='pushdb', passwd='SkYWOrTh$TcOs',
    #                db='proactive_service_data',
    #                port=3306)  # 1为连接池里的最少连接数
    # 测试环境
    pool = PooledDB(pymysql, 1, host='172.20.154.103', user='test_dmp', passwd='DghHC3lFM1KzT3ZJ',
                    db='proactive_service_data', port=3407)  # 1为连接池里的最少连接数
    # pool = PooledDB(pymysql,1,host='127.0.0.1',user='root',passwd='root',db='life_assistant_data',port=3306) # 5为连接池里的最少连接数
    conn = pool.connection()
    cur = conn.cursor()
    return conn, cur

# redis
def getReidsConnection():
    redis_info = {
        "host": "172.20.151.90",
        "password": "Coocaa2022",
        "port": 6379,
        "db": 8
    }

    r = redis.Redis(**redis_info, decode_responses=True)
    return r

#根据传入的SQL 返回执行SQL返回的数量
def selectNumBySql(sql):
    conn, cur = getDataConnection()
    #print(sql)
    cur.execute(sql)
    numResult = cur.fetchone()
    num = 0
    if numResult is not None:
        num = numResult[0]
    return num


#根据月份 获取本月指定指标的 总和
#tableName 指定的报表
#columnName 指定的指标字段
#date 月份
def getSumNumByTableColumnNameAndDate(tableName,columnName,date):
    conn, cur = getDataConnection()
    selectNumSql = f"""
                     select sum({columnName}) from {tableName} t where 1=1   
                     """;
    if len(date) >0:
        selectNumSql = f"""{selectNumSql} and SUBSTR( t.ref_date, 1,7) = '{date}' and LENGTH(t.ref_date) = 10  """
    cur.execute(selectNumSql)
    #print(selectNumSql);
    numResult = cur.fetchone()
    num = 0
    if numResult!= None:
        num = numResult[0]
    if num is None:
        return 0
    return num

#计算百分比 保留两位小数  如:34.88%
# X为分子 Y为分母
def getRateByXY(X,Y):
    rate = 0
    if(X is None or Y is None):
        return rate
    if Y != 0:
        if X > Y:
            rate = 100
        else:
            rate = round(X / Y, 4) * 100
    return rate

# 记录 数据分析-睡觉提醒-服务推送成功率 analysis_sjtx_push_rate
def insertPushRate(date):
    try:
        conn, cur = getDataConnection()
        #判断 analysis_sjtx_push_rate 表是否已经存在当天 数据
        countNum = selectNumBySql(
            f"select count(1) from analysis_sjtx_push_rate t where 1=1 and  t.ref_date = '{date}'")
        # 获取当日早间开机弹窗数
        alertNum = selectNumBySql(f"""
            select count(DISTINCT(t.active_id)) from log_common_service_{yyMMddDate} t where t.`key` = 'askShow'   and JSON_EXTRACT(msg,"$.service") ='SJTX'
            and  JSON_EXTRACT(msg,"$.result") = 1
            """)
        # 获取所有早间开机弹窗数
        cumulatePushNum = selectNumBySql(
            f"select ifnull(sum(alert_num),0) from analysis_sjtx_push_rate t where 1=1 and LENGTH(t.ref_date) = 10  and t.ref_date <= '{date}'")
        #计算 收到服务弹窗推送并在当天成功开启服务的用户量
        baseSql = f""" 
                  from log_common_service_{yyMMddDate} t where t.`key` = 'openServer2' and JSON_EXTRACT(t.msg,'$.serId') = 'SJTX'  and t.active_id in (
            select tt.active_id from log_common_service_{yyMMddDate} tt where tt.`key` = 'askShow' and JSON_EXTRACT(tt.msg,'$.service') ='SJTX' and JSON_EXTRACT(tt.msg,'$.result') =1)
            """
        selectOpenNumSql = f"""
                    select count(DISTINCT(t.active_id)) {baseSql}"""
        openNum = selectNumBySql(selectOpenNumSql)
        #计算当天成功开启服务 并且创建定时任务的数量
        selectCreateTaskSql = f"""
                    select count(DISTINCT(tt.active_id))  from log_common_service_{yyMMddDate} tt where tt.`key` = 'srCreateTaskResult'  
        and tt.active_id in (select DISTINCT(t.active_id) {baseSql})
                """
        createTaskNum = selectNumBySql(selectCreateTaskSql)
        # 计算早间开机推送成功率
        pushRate = getRateByXY(openNum, alertNum)

        if countNum == 0:
            logDetail = f"{date}日新增-早间开机弹窗推送数量为{alertNum},累计推送数为{cumulatePushNum},当天成功开启服务的设备量为{openNum},成功率为{pushRate}," \
                        f"并在当天创建定时器任务数为{createTaskNum}"
            print(logDetail)
            # 插入
            insertSql = f"""insert into analysis_sjtx_push_rate(alert_num,cumulate_num,open_num,success_rate,create_task_num,ref_date) 
                value({alertNum},{cumulatePushNum},{openNum},{pushRate},{createTaskNum},'{date}')"""
            cur.execute(insertSql)
        else:
            logDetail = f"{date}日更新-早间开机弹窗推送数量为{alertNum},累计推送数为{cumulatePushNum},当天成功开启服务的设备量为{openNum},成功率为{pushRate}," \
                        f"并在当天创建定时器任务数为{createTaskNum}"
            print(logDetail)
            # 更新
            updateSql = f"""
                update analysis_sjtx_push_rate set alert_num = {alertNum} ,cumulate_num = {cumulatePushNum},open_num = {openNum} ,success_rate = {pushRate}, create_task_num = {createTaskNum},
                update_time = now()  where ref_date = '{date}' 
            """
            cur.execute(updateSql)
        saveLog(logDetail, date)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        conn.close()

# 记录 数据分析-早间开机-服务推送成功率 analysis_sjtx_push_rate --月度
def insertPushRateMonth(toMonthDay):
    try:
        conn, cur = getDataConnection()
        #判断 analysis_zjkj_push_rate 表是否已经存在当天 数据
        countNum = selectNumBySql(
            f"select count(1) from analysis_sjtx_push_rate t where 1=1 and  t.ref_date = '{toMonthDay}'")
        # 获取本月早间开机弹窗数
        alertNum = getSumNumByTableColumnNameAndDate('analysis_sjtx_push_rate', 'alert_num', toMonthDay)
        # 获取当月所有早间开机弹窗数
        cumulatePushNum = selectNumBySql(
            f"select ifnull(sum(alert_num),0) from analysis_sjtx_push_rate t where 1=1 and LENGTH(t.ref_date) = 10  ")
        #计算 收到服务弹窗推送并在当天成功开启服务的用户量 --月度累计
        openNum = getSumNumByTableColumnNameAndDate('analysis_sjtx_push_rate', 'open_num', toMonthDay)
        # 获取本月成功开启服务且创建定时任务的数量
        createTaskNum = getSumNumByTableColumnNameAndDate('analysis_sjtx_push_rate', 'create_task_num', toMonthDay)
        # 计算早间开机推送成功率
        pushRate = getRateByXY(openNum, alertNum)


        if countNum == 0:
            logDetail = f"{toMonthDay}月份新增-早间开机弹窗推送数量为{alertNum},累计推送数为{cumulatePushNum},当天成功开启服务的设备量为{openNum},成功率为{pushRate}," \
                        f"并在当月创建定时器任务数为{createTaskNum}"
            print(logDetail)
            # 插入
            insertSql = f"""insert into analysis_sjtx_push_rate(alert_num,cumulate_num,open_num,success_rate,create_task_num,ref_date) 
                value({alertNum},{cumulatePushNum},{openNum},{pushRate},{createTaskNum},'{toMonthDay}')"""
            cur.execute(insertSql)
        else:
            logDetail = f"{toMonthDay}月份更新-早间开机弹窗推送数量为{alertNum},累计推送数为{cumulatePushNum},当天成功开启服务的设备量为{openNum},成功率为{pushRate}," \
                        f"并在当月创建定时器任务数为{createTaskNum}"
            print(logDetail)
            # 更新
            updateSql = f"""
                update analysis_sjtx_push_rate set alert_num = {alertNum} ,cumulate_num = {cumulatePushNum},open_num = {openNum} ,success_rate = {pushRate}, create_task_num = {createTaskNum},
                ,update_time = now()  where ref_date = '{toMonthDay}' 
            """
            cur.execute(updateSql)
        saveLog(logDetail, toMonthDay)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        conn.close()

# 获取当前日期
def todayYMD():
    today = datetime.datetime.now()-1
    # 获取想要的日期的时间
    re_date = (today).strftime('%Y-%m-%d')
    return re_date


# 获取前1天或N天的日期，beforeOfDay=1：前1天；beforeOfDay=N：前N天
def getdate(beforeOfDay):
    today = datetime.datetime.now()
    # 计算偏移量
    offset = datetime.timedelta(days=-beforeOfDay)
    # 获取想要的日期的时间
    re_date = (today + offset).strftime('%Y-%m-%d')
    return re_date

# 获取前1天或N天的日期，beforeOfDay=1：前1天；beforeOfDay=N：前N天
def getMonthdate(beforeOfDay):
    today = datetime.datetime.now()
    # 计算偏移量
    offset = datetime.timedelta(days=-beforeOfDay)
    # 获取想要的日期的时间
    re_date = (today + offset).strftime('%Y-%m')
    return re_date

#如 2022年12月13号 获取 221213 作为分表后缀
def getdateyyMMdd(beforeOfDay):
    today = datetime.datetime.now()
    # 计算偏移量
    offset = datetime.timedelta(days=-beforeOfDay)
    # 获取想要的日期的时间
    re_date = (today + offset).strftime('%Y%m%d')
    re_date = re_date[2:]
    return re_date

def saveLog(detail,ref_date):
    try:
        conn, cur = getDataConnection()
        jobId = 29
        jobName = '核心指标-睡觉提醒-推送成功率'
        tableName = 'analysis_sjtx_push_rate'
        # 插入日志表
        saveLogSql = f"""insert into log_xxljob_analysis (job_id,job_name,detail,table_name,ref_date) 
                                                        value({jobId},'{jobName}','{detail}','{tableName}','{ref_date}')"""
        cur.execute(saveLogSql)
        conn.commit()
    except Exception as e:
        print(e)
    finally:
        cur.close()
        conn.close()

if __name__ == '__main__':
    #前移的天数。1表示昨天
    d = 1
    date = getdate(d)
    yyMMddDate = getdateyyMMdd(d)
    beforeDate = getdate(d+1)
    monthdate = getMonthdate(d)
    #today = "2021-12-13"
    print (f"{date} 日期,更新<数据分析-睡觉提醒-服务推送成功率>报表")
    insertPushRate(date)
    insertPushRateMonth(monthdate)

